package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ConsumerMapper;
import com.goods.business.service.ConsumerService;
import com.goods.common.model.business.Consumer;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ConsumerServiceImpl implements ConsumerService {
    @Autowired
    private ConsumerMapper consumerMapper;

    //加载物资去处列表
    @Override
    public PageVO<ConsumerVO> findAll(Map<String, Object> map) {
        PageHelper.startPage(Integer.valueOf((String)map.get("pageNum")),
                Integer.valueOf((String) map.get("pageSize")));
        Example example = new Example(Consumer.class);
        Example.Criteria criteria = example.createCriteria();

        String address = (String) map.get("address");
        if (address != null && !"".equals(address)){
            criteria.andLike("address","%" + address + "%");
        }

        String contact = (String) map.get("contact");
        if (contact != null && !"".equals(contact)){
            criteria.andEqualTo("contact",contact);
        }

        String name = (String) map.get("name");
        if (name != null && !"".equals(name)){
            criteria.andLike("name","%" + name + "%");
        }
        List<Consumer> list = consumerMapper.selectByExample(example);
        PageInfo<Consumer> pageInfo = new PageInfo<>(list);

        List<ConsumerVO> listVO = list.stream().map(consumer -> {
            ConsumerVO consumerVO = new ConsumerVO();
            BeanUtils.copyProperties(consumer, consumerVO);
            return consumerVO;
        }).collect(Collectors.toList());
        return new PageVO<ConsumerVO>(pageInfo.getTotal(),listVO);
    }

    //添加物资去处列表
    @Override
    public void add(ConsumerVO consumerVO) {
        Consumer consumer = new Consumer();
        Date date = new Date();
        BeanUtils.copyProperties(consumerVO,consumer);
        consumer.setCreateTime(date);
        consumer.setModifiedTime(date);
        consumerMapper.insert(consumer);
    }

    //打开编辑页面,并且回显
    @Override
    public ConsumerVO edit(Long id) {
        Consumer consumer = new Consumer();
        consumer.setId(id);
        Consumer consumer1 = consumerMapper.selectByPrimaryKey(consumer);
        ConsumerVO consumerVO = new ConsumerVO();
        BeanUtils.copyProperties(consumer1,consumerVO);
        return consumerVO;
    }

    //更新用户
    @Override
    public void update(ConsumerVO consumerVO) {
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerVO,consumer);
        consumerMapper.updateByPrimaryKey(consumer);
    }

    //删除
    @Override
    public void delete(Long id) {
        Consumer consumer = new Consumer();
        consumer.setId(id);
        consumerMapper.deleteByPrimaryKey(consumer);
    }

    @Override
    public List<ConsumerVO> fdAll() {
        List<Consumer> list = consumerMapper.selectAll();
        List<ConsumerVO> vo = list.stream().map(consumer -> {
            ConsumerVO consumerVO = new ConsumerVO();
            BeanUtils.copyProperties(consumer, consumerVO);
            return consumerVO;
        }).collect(Collectors.toList());
        return vo;
    }


}
